using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Windows.Forms.ColorControls;

namespace System.Drawing.Drawing2D.Design
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    internal class FillEditor : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button buttonBrowse;
		private System.Windows.Forms.ColorControls.TabbedColorPicker colorPicker1;
		private System.Windows.Forms.ColorControls.TabbedColorPicker colorPicker2;
        private System.Windows.Forms.ComboBox comboImageWrap;
        private System.Windows.Forms.ComboBox comboPattern;
        private System.Windows.Forms.ComboBox comboStyle;

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label10;
        private System.Windows.Forms.Label label11;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.Label label7;
        private System.Windows.Forms.OpenFileDialog openFileDialog1;
        private System.Windows.Forms.OpenFileDialog openFileDialog2;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Panel panelGradient;
        private System.Windows.Forms.Panel panelImage;
        private System.Windows.Forms.Panel panelPattern;
        private System.Windows.Forms.PictureBox pictureImage;
        private System.Windows.Forms.Shape shapeDemo;
        private System.Windows.Forms.TrackBar trackBarCenter;
        private TrackBar trackBarTransparency;
        private System.Windows.Forms.NumericUpDown upDnAngle;

        /// <summary>
        /// 
        /// </summary>
        public FillEditor()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            // Create shape
            this.shapeDemo = new System.Windows.Forms.Shape();
            shapeDemo.BeginInit();
            shapeDemo.Name = "shapeDemo";
            shapeDemo.Line.Width = 0;
            shapeDemo.Dock = DockStyle.Fill;
            panel1.Controls.Add(shapeDemo);
            shapeDemo.EndInit();

            foreach (string s in Enum.GetNames(typeof(FillStyle)))
                comboStyle.Items.Add(s);
            foreach (string s in Enum.GetNames(typeof(System.Drawing.Drawing2D.WrapMode)))
                comboImageWrap.Items.Add(s);
            foreach (string s in Enum.GetNames(typeof(System.Drawing.Drawing2D.HatchStyle)))
                comboPattern.Items.Add(s);
            SetControls();
        }

        /// <summary>
        /// 
        /// </summary>
        public Fill Fill
        {
            get { return shapeDemo.Fill; }
            set
            {
                shapeDemo.Fill = value;
                SetControls();
            }
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        private void buttonBrowse_Click(object sender, System.EventArgs e)
        {
            if (DialogResult.OK == openFileDialog1.ShowDialog(this))
            {
                try
                {
                    Fill.Image = Image.FromFile(openFileDialog1.FileName);
                    pictureImage.Image = Fill.Image;
                }
                catch
                {
                }
            }
        }

        private void cancelButton_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }

		private void colorPicker1_ColorChanged(object sender, System.Windows.Forms.ColorControls.ColorChangedEventArgs e)
        {
            Fill.Color1 = e.Color;
        }

		private void colorPicker2_ColorChanged(object sender, System.Windows.Forms.ColorControls.ColorChangedEventArgs e)
        {
            Fill.Color2 = e.Color;
        }

        private void comboImageWrap_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            Fill.ImageWrapMode = (System.Drawing.Drawing2D.WrapMode)Enum.Parse(typeof(System.Drawing.Drawing2D.WrapMode), (string)comboImageWrap.Items[comboImageWrap.SelectedIndex]);
        }

        private void comboPattern_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            Fill.Pattern = (System.Drawing.Drawing2D.HatchStyle)Enum.Parse(typeof(System.Drawing.Drawing2D.HatchStyle), (string)comboPattern.Items[comboPattern.SelectedIndex]);
        }

        private void comboStyle_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            shapeDemo.Fill.Style = (FillStyle)Enum.Parse(typeof(FillStyle), (string)comboStyle.Items[comboStyle.SelectedIndex]);
            panelGradient.Visible = panelImage.Visible = panelPattern.Visible = false;
            colorPicker1.Enabled = colorPicker2.Enabled = trackBarTransparency.Enabled = false;
            switch (shapeDemo.Fill.Style)
            {
                case FillStyle.Solid:
                    colorPicker1.Enabled = trackBarTransparency.Enabled = true;
                    break;
                case FillStyle.Image:
                    panelImage.Visible = true;
                    break;
                case FillStyle.Pattern:
                    colorPicker1.Enabled = colorPicker2.Enabled = trackBarTransparency.Enabled = true;
                    panelPattern.Visible = true;
                    break;
                case FillStyle.LinearGradient:
                case FillStyle.ABALinearGradient:
                case FillStyle.RadialGradient:
                case FillStyle.RectangularGradient:
                case FillStyle.PathGradient:
                    colorPicker1.Enabled = colorPicker2.Enabled = trackBarTransparency.Enabled = true;
                    panelGradient.Visible = true;
                    colorPicker2.Enabled = true;
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.label2 = new System.Windows.Forms.Label();
            this.comboStyle = new System.Windows.Forms.ComboBox();
            this.label4 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            this.panelGradient = new System.Windows.Forms.Panel();
            this.label5 = new System.Windows.Forms.Label();
            this.label6 = new System.Windows.Forms.Label();
            this.upDnAngle = new System.Windows.Forms.NumericUpDown();
            this.trackBarCenter = new System.Windows.Forms.TrackBar();
            this.panelImage = new System.Windows.Forms.Panel();
            this.pictureImage = new System.Windows.Forms.PictureBox();
            this.buttonBrowse = new System.Windows.Forms.Button();
            this.label10 = new System.Windows.Forms.Label();
            this.comboImageWrap = new System.Windows.Forms.ComboBox();
            this.label1 = new System.Windows.Forms.Label();
			this.colorPicker2 = new System.Windows.Forms.ColorControls.TabbedColorPicker();
            this.openFileDialog2 = new System.Windows.Forms.OpenFileDialog();
			this.colorPicker1 = new System.Windows.Forms.ColorControls.TabbedColorPicker();
            this.label3 = new System.Windows.Forms.Label();
            this.panelPattern = new System.Windows.Forms.Panel();
            this.comboPattern = new System.Windows.Forms.ComboBox();
            this.label11 = new System.Windows.Forms.Label();
            this.panel1 = new System.Windows.Forms.Panel();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.trackBarTransparency = new System.Windows.Forms.TrackBar();
            this.panelGradient.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.upDnAngle)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.trackBarCenter)).BeginInit();
            this.panelImage.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureImage)).BeginInit();
            this.panelPattern.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.trackBarTransparency)).BeginInit();
            this.SuspendLayout();
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(10, 42);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(43, 13);
            this.label2.TabIndex = 47;
            this.label2.Text = "Color 1:";
            //
            // comboStyle
            //
            this.comboStyle.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboStyle.Location = new System.Drawing.Point(90, 10);
            this.comboStyle.Name = "comboStyle";
            this.comboStyle.Size = new System.Drawing.Size(184, 21);
            this.comboStyle.TabIndex = 48;
            this.comboStyle.SelectedIndexChanged += new System.EventHandler(this.comboStyle_SelectedIndexChanged);
            //
            // label4
            //
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(10, 106);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(75, 13);
            this.label4.TabIndex = 49;
            this.label4.Text = "Transparency:";
            //
            // label7
            //
            this.label7.AutoSize = true;
            this.label7.Location = new System.Drawing.Point(10, 74);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(43, 13);
            this.label7.TabIndex = 45;
            this.label7.Text = "Color 2:";
            //
            // panelGradient
            //
            this.panelGradient.Controls.Add(this.label5);
            this.panelGradient.Controls.Add(this.label6);
            this.panelGradient.Controls.Add(this.upDnAngle);
            this.panelGradient.Controls.Add(this.trackBarCenter);
            this.panelGradient.Location = new System.Drawing.Point(10, 138);
            this.panelGradient.Name = "panelGradient";
            this.panelGradient.Size = new System.Drawing.Size(264, 64);
            this.panelGradient.TabIndex = 51;
            this.panelGradient.Visible = false;
            //
            // label5
            //
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(0, 32);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(41, 13);
            this.label5.TabIndex = 29;
            this.label5.Text = "Center:";
            //
            // label6
            //
            this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(0, 0);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(37, 13);
            this.label6.TabIndex = 27;
            this.label6.Text = "Angle:";
            //
            // upDnAngle
            //
            this.upDnAngle.Location = new System.Drawing.Point(80, 0);
            this.upDnAngle.Maximum = new decimal(new int[] {
            360,
            0,
            0,
            0});
            this.upDnAngle.Minimum = new decimal(new int[] {
            360,
            0,
            0,
            -2147483648});
            this.upDnAngle.Name = "upDnAngle";
            this.upDnAngle.Size = new System.Drawing.Size(56, 20);
            this.upDnAngle.TabIndex = 28;
            this.upDnAngle.TextChanged += new System.EventHandler(this.upDnAngle_ValueChanged);
            this.upDnAngle.ValueChanged += new System.EventHandler(this.upDnAngle_ValueChanged);
            //
            // trackBarCenter
            //
            this.trackBarCenter.LargeChange = 10;
            this.trackBarCenter.Location = new System.Drawing.Point(72, 28);
            this.trackBarCenter.Maximum = 100;
            this.trackBarCenter.Minimum = -100;
            this.trackBarCenter.Name = "trackBarCenter";
            this.trackBarCenter.Size = new System.Drawing.Size(104, 45);
            this.trackBarCenter.TabIndex = 30;
            this.trackBarCenter.TickFrequency = 20;
            this.trackBarCenter.Scroll += new System.EventHandler(this.trackBarCenter_Scroll);
            //
            // panelImage
            //
            this.panelImage.Controls.Add(this.pictureImage);
            this.panelImage.Controls.Add(this.buttonBrowse);
            this.panelImage.Controls.Add(this.label10);
            this.panelImage.Controls.Add(this.comboImageWrap);
            this.panelImage.Controls.Add(this.label1);
            this.panelImage.Location = new System.Drawing.Point(10, 138);
            this.panelImage.Name = "panelImage";
            this.panelImage.Size = new System.Drawing.Size(264, 64);
            this.panelImage.TabIndex = 52;
            this.panelImage.Visible = false;
            //
            // pictureImage
            //
            this.pictureImage.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.pictureImage.Location = new System.Drawing.Point(80, 0);
            this.pictureImage.Name = "pictureImage";
            this.pictureImage.Size = new System.Drawing.Size(24, 24);
            this.pictureImage.TabIndex = 35;
            this.pictureImage.TabStop = false;
            //
            // buttonBrowse
            //
            this.buttonBrowse.Location = new System.Drawing.Point(112, 0);
            this.buttonBrowse.Name = "buttonBrowse";
            this.buttonBrowse.Size = new System.Drawing.Size(64, 24);
            this.buttonBrowse.TabIndex = 34;
            this.buttonBrowse.Text = "&Browse";
            this.buttonBrowse.Click += new System.EventHandler(this.buttonBrowse_Click);
            //
            // label10
            //
            this.label10.AutoSize = true;
            this.label10.Location = new System.Drawing.Point(0, 32);
            this.label10.Name = "label10";
            this.label10.Size = new System.Drawing.Size(65, 13);
            this.label10.TabIndex = 31;
            this.label10.Text = "Image wrap:";
            //
            // comboImageWrap
            //
            this.comboImageWrap.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboImageWrap.ItemHeight = 13;
            this.comboImageWrap.Location = new System.Drawing.Point(80, 32);
            this.comboImageWrap.Name = "comboImageWrap";
            this.comboImageWrap.Size = new System.Drawing.Size(184, 21);
            this.comboImageWrap.TabIndex = 32;
            this.comboImageWrap.SelectedIndexChanged += new System.EventHandler(this.comboImageWrap_SelectedIndexChanged);
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(0, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(39, 13);
            this.label1.TabIndex = 31;
            this.label1.Text = "Image:";
            //
            // colorPicker2
            //
            this.colorPicker2.Location = new System.Drawing.Point(90, 74);
            this.colorPicker2.Name = "colorPicker2";
            this.colorPicker2.Size = new System.Drawing.Size(184, 21);
            this.colorPicker2.TabIndex = 56;
			this.colorPicker2.ColorChanged += new System.Windows.Forms.ColorControls.ColorChangedEventHandler(this.colorPicker2_ColorChanged);
            //
            // colorPicker1
            //
            this.colorPicker1.Location = new System.Drawing.Point(90, 42);
            this.colorPicker1.Name = "colorPicker1";
            this.colorPicker1.Size = new System.Drawing.Size(184, 21);
            this.colorPicker1.TabIndex = 55;
			this.colorPicker1.ColorChanged += new System.Windows.Forms.ColorControls.ColorChangedEventHandler(this.colorPicker1_ColorChanged);
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(10, 10);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(46, 13);
            this.label3.TabIndex = 46;
            this.label3.Text = "Fill style:";
            //
            // panelPattern
            //
            this.panelPattern.Controls.Add(this.comboPattern);
            this.panelPattern.Controls.Add(this.label11);
            this.panelPattern.Location = new System.Drawing.Point(10, 138);
            this.panelPattern.Name = "panelPattern";
            this.panelPattern.Size = new System.Drawing.Size(264, 64);
            this.panelPattern.TabIndex = 53;
            this.panelPattern.Visible = false;
            //
            // comboPattern
            //
            this.comboPattern.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboPattern.Location = new System.Drawing.Point(80, 0);
            this.comboPattern.Name = "comboPattern";
            this.comboPattern.Size = new System.Drawing.Size(184, 21);
            this.comboPattern.TabIndex = 31;
            this.comboPattern.SelectedIndexChanged += new System.EventHandler(this.comboPattern_SelectedIndexChanged);
            //
            // label11
            //
            this.label11.AutoSize = true;
            this.label11.Location = new System.Drawing.Point(0, 0);
            this.label11.Name = "label11";
            this.label11.Size = new System.Drawing.Size(44, 13);
            this.label11.TabIndex = 30;
            this.label11.Text = "Pattern:";
            //
            // panel1
            //
            this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.panel1.Location = new System.Drawing.Point(290, 10);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(100, 100);
            this.panel1.TabIndex = 54;
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(312, 144);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 57;
            this.button1.Text = "OK";
            this.button1.Click += new System.EventHandler(this.okButton_Click);
            //
            // button2
            //
            this.button2.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.button2.Location = new System.Drawing.Point(312, 176);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 58;
            this.button2.Text = "Cancel";
            this.button2.Click += new System.EventHandler(this.cancelButton_Click);
            //
            // trackBarTransparency
            //
            this.trackBarTransparency.LargeChange = 32;
            this.trackBarTransparency.Location = new System.Drawing.Point(82, 101);
            this.trackBarTransparency.Maximum = 255;
            this.trackBarTransparency.Name = "trackBarTransparency";
            this.trackBarTransparency.Size = new System.Drawing.Size(104, 45);
            this.trackBarTransparency.TabIndex = 50;
            this.trackBarTransparency.TickFrequency = 32;
            this.trackBarTransparency.Scroll += new System.EventHandler(this.trackBarTransparency_ValueChanged);
            //
            // FillEditor
            //
            this.AcceptButton = this.button1;
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.CancelButton = this.button2;
            this.ClientSize = new System.Drawing.Size(400, 213);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.comboStyle);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label7);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.colorPicker2);
            this.Controls.Add(this.colorPicker1);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.panelGradient);
            this.Controls.Add(this.panelImage);
            this.Controls.Add(this.panelPattern);
            this.Controls.Add(this.trackBarTransparency);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.Name = "FillEditor";
            this.Text = "Fill";
            this.panelGradient.ResumeLayout(false);
            this.panelGradient.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.upDnAngle)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.trackBarCenter)).EndInit();
            this.panelImage.ResumeLayout(false);
            this.panelImage.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureImage)).EndInit();
            this.panelPattern.ResumeLayout(false);
            this.panelPattern.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.trackBarTransparency)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        private void okButton_Click(object sender, System.EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        /// <summary>
        /// 
        /// </summary>
        private void SetControls()
        {
            comboStyle.SelectedIndex = (int)comboStyle.FindString(Fill.Style.ToString());
            colorPicker1.Color = Fill.Color1;
            colorPicker2.Color = Fill.Color2;
            trackBarTransparency.Value = Fill.Transparency;
            pictureImage.Image = Fill.Image;
            comboImageWrap.SelectedIndex = (int)comboImageWrap.FindString(Fill.ImageWrapMode.ToString());
            comboPattern.SelectedIndex = (int)comboPattern.FindString(Fill.Pattern.ToString());
            upDnAngle.Value = (decimal)Fill.GradientAngle;
            trackBarCenter.Value = (int)(Fill.GradientCenter*100);
        }

        private void trackBarCenter_Scroll(object sender, System.EventArgs e)
        {
            Fill.GradientCenter = (float)trackBarCenter.Value/100f;
        }

        private void trackBarTransparency_ValueChanged(object sender, System.EventArgs e)
        {
            Fill.Transparency = (int)trackBarTransparency.Value;
        }

        private void upDnAngle_ValueChanged(object sender, System.EventArgs e)
        {
            Fill.GradientAngle = (float)upDnAngle.Value;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    internal class FillTypeEditor : System.Drawing.Design.UITypeEditor
    {
        /// <summary>
        /// 
        /// </summary>
        public FillTypeEditor()
            : base()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="provider"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
        {
            if (context != null && context.Instance != null && provider != null && value is Fill)
            {
                IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

                if (edSvc != null)
                {
                    FillEditor ed = new FillEditor();
                    ed.Fill = (Fill)value;
                    if (edSvc.ShowDialog(ed) == DialogResult.OK)
                        value = ed.Fill;
                }
            }

            return value;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            return System.Drawing.Design.UITypeEditorEditStyle.Modal;
        }
    }
}